<?php


namespace app\index\service;


use app\index\inter\OrderInter;
use wxpay\JsapiPay;

class OrderService implements OrderInter
{
    public function pay($a)
    {
        $params = [
            'body'         => '医约宝-医疗服务(方克鑫科技)',
            'out_trade_no' => mt_rand().time(),
            'total_fee'    => $a['money']*100,
        ];
        $openId = $a['openid'];
        $result = JsapiPay::getParams($params, $openId);
        return $result;
    }
//    public function push1($a)
//    {
//        $params = [
//            'touser' => $a['openid'],
//            "template_id"=>'PHznG14PsKwViS7A7xP7p4zf6PyRY0daVwwDLLxtW4g',
//            "page" =>'pages/order/order',
//            "miniprogram_state"=>"developer",
//            "lang"=>"zh_CN",
//            "data"=>[
//                "thing1"=>[
//                    "value"=>'您的订单已经被接单'
//                ],
//                "name3"=>[
//                    "value"=>$a['name']
//                ],
//                "thing4"=>[
//                    "value"=>"请及时支付,未支付将在10分钟后取消"
//                ],
//                "thing2"=>[
//                    "value"=>"接单技师电话".$a['tel']
//                ]
//            ]
//        ];
//        $Token = $this->getToken();
//        $t= json_decode($Token);
//        $access_token =  $t->access_token;
//        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $access_token;
//        $result = send_post($url,$params);
//        return $result;
//    }
    public function push2($a)
    {
        $params = [
            'touser' => $a['openid'],
            "template_id"=>'Bggno_EEVea-ey6v8Bj_p0AKjV5Q2y8ysymfwJrjeEw',
            "page" =>'pages/order/order',
            "page" =>$a['page'],
            "lang"=>"zh_CN",
            "data"=>[
                "thing1"=>[
                    "value"=>$a['name1']
                ],
                "thing2"=>[
                    "value"=>$a['name2']
                ],
                "thing4"=>[
                    "value"=>$a['name3']
                ]
            ]
        ];
        $Token = $this->getToken();
        $t= json_decode($Token);
        $access_token =  $t->access_token;
        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $access_token;
        $result = send_post($url,$params);
        return $result;
    }
    //获取小程序的access_token
    public function getToken(){
        //开发者使用登陆凭证 code 获取 session_key 和 openid
        $APPID = config('APPID');//自己配置
        $AppSecret =config('AppSecret');//自己配置
        $url = "https://api.weixin.qq.com/cgi-bin/token?appid=" . $APPID . "&secret=" . $AppSecret . "&grant_type=client_credential";
        $arr = get_curl_json($url);  // 一个使用curl实现的get方法请求
        return json_encode($arr);

    }
    //获取所有的openid
    public function getAllOnenId(){
        $Token2 = $this->getTokenG();
        $t2= json_decode($Token2,true);
        $access_token2 =  $t2['access_token'];
        $allOpenId= $this->getAllOpenid($access_token2);
        $t3= json_decode($allOpenId,true);
        $all = $t3["data"]['openid'];
        return $all;
    }
    //获取公众号的access_token
    public function getTokenG(){
        //开发者使用登陆凭证 code 获取 session_key 和 openid
        $APPID = "wx7316676cbe8e5c4b";//自己配置
        $AppSecret ="1a6f4fb352d02bf86c2f1a8f23641376";//自己配置
        $url = "https://api.weixin.qq.com/cgi-bin/token?appid=" . $APPID . "&secret=" . $AppSecret . "&grant_type=client_credential";
        $arr = get_curl_json($url);  // 一个使用curl实现的get方法请求
        return json_encode($arr);

    }
    //获取所有公众号用户openid
    public  function getAllOpenid($t){
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" . $t ;
        $arr = get_curl_json($url);  // 一个使用curl实现的get方法请求
        return json_encode($arr);
    }
}